package com.jd.org.service.impl;

import com.jd.basic.exception.BusinessException;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.BaiduAuditUtils;
import com.jd.basic.util.MD5Utils;
import com.jd.basic.util.StrUtils;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.domain.ShopAuditLog;
import com.jd.org.dto.ShopDto;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.mapper.ShopAuditLogMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.org.service.IShopService;
import com.jd.user.domain.Logininfo;
import com.jd.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 1.校验 2.格式通过正则表达式 3.确认密码校验
        // 空值校验
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能够为空");
        }
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if (dbShop != null){
            throw new BusinessException("店铺已经入驻，忘记密码请你联系管理员");
        }
        // 百度AI智能审核
        Boolean boo1 = BaiduAuditUtils.TextCensor(shop.getName() + shop.getAddress());
        if(!boo1){
            throw  new BusinessException("店铺名称和地址不合法");
        }
        if (!StringUtils.isEmpty(shop.getLogo())) {
            String url = "http://123.207.27.208" + shop.getLogo(); //常量
            Boolean boo2 = BaiduAuditUtils.ImgCensor(url);
            if (!boo2) {
                throw new BusinessException("logo违规");
            }
        }
        // 添加管理员
        Employee admin = shop.getAdmin();

        // 处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0); // 管理员
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);

        admin.setLogininfo_id(logininfo.getId());

        employeeMapper.save(admin);

        // 将自增id设置到t_shop
        shop.setAdminId(admin.getId());

        // 添加shop
        shopMapper.save(shop);

        // 将shop_id更新到t_employee
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);


    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);


        // 2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.save(shopAuditLog);

        // 3.发送激活邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom("2671712858@qq.com");
        // 给管理员发送邮件
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        helper.setSubject("店铺激活成功");
        helper.setText("<h1>湖人队总冠军！！</h1><h3><a href='http://localhost:8080/shop/active/" +shopAuditLog.getShopId()+"'>点击该链接激活</a></h3>"
                +"<h3><a href='http://localhost:8081/#/login'>点击链接进入登录页面</a></h3>",true);
        javaMailSender.send(message);

    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送激活邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        helper.setFrom("2671712858@qq.com");

        //给管理员发送邮件
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail());
        helper.setSubject("店铺激活邮件!!!");
        helper.setText("<h1>店铺激活失败!!!</h1><h3><a href='http://localhost:8081/#/register'>点击该链接重新入驻</a></h3>",true);
        javaMailSender.send(message);

    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    // 激活成功修改状态
    @Override
    public void active(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(3);
        shopMapper.update(shop);
    }


}
